package ie.dcu.spca.view.product;

import java.util.Map;

import ie.dcu.spca.Main;
import ie.dcu.spca.R;
import ie.dcu.spca.data.BasicDataManager;
import ie.dcu.spca.data.CategoryDataManager;
import ie.dcu.spca.data.PriceUnitDataManager;
import ie.dcu.spca.model.bean.Product;
import ie.dcu.spca.view.CRUDDialog;
import ie.dcu.spca.view.DataList;
import android.app.Activity;
import android.widget.EditText;
import android.widget.Spinner;

public class ProductDetailForm {
    private static CategoryDataManager categoryManager;
    private static PriceUnitDataManager unitManager;

    private Activity parent;

    public ProductDetailForm(Activity parent) {
        this.parent = parent;
        categoryManager = Main.dataManager.categoryDataManager();
        unitManager = Main.dataManager.priceUnitDataManager();
    }

    private String name;
    private String barcode;
    private double price;
    private int categoryId;
    private int unitId;

    public String getName() {
        return name;
    }

    public String getBarcode() {
        return barcode;
    }

    public double getPrice() {
        return price;
    }

    public int getCategoryId() {
        return categoryId;
    }

    public int getUnitId() {
        return unitId;
    }

    public EditText getEtBarcode() {
        return etBarcode;
    }

    private EditText etName;
    private EditText etBarcode;
    private EditText etPrice;
    private Spinner spCategory;
    private Spinner spUnit;

    public void bindWidgetOnCreate() {
        etName = (EditText) parent.findViewById(R.id.nameEditText);
        etBarcode = (EditText) parent.findViewById(R.id.barcodeEditText);
        etPrice = (EditText) parent.findViewById(R.id.priceEditText);

        spCategory = (Spinner) parent.findViewById(R.id.categorySpinner);
        spCategory.setAdapter(DataList.getIdNameSimpleAdapter(parent,
                categoryManager.getIdNameMapList()));

        spUnit = (Spinner) parent.findViewById(R.id.unitSpinner);
        spUnit.setAdapter(DataList.getIdNameSimpleAdapter(parent, unitManager
                .getIdNameMapList()));

    }

    public void initializeValuesFromWidget() {
        name = etName.getText().toString();
        barcode = etBarcode.getText().toString();
        price = Double.parseDouble(etPrice.getText().toString());
        categoryId = (Integer) ((Map<String, Object>) spCategory
                .getSelectedItem()).get(BasicDataManager.ID);
        unitId = (Integer) ((Map<String, Object>) spUnit.getSelectedItem())
                .get(BasicDataManager.ID);
    }

    public void updateBeanWidgetInitialize(Product product) {

        name = product.getName();
        etName.setText(name);
        barcode = product.getBarcode();
        etBarcode.setText(barcode);
        price = product.getPrice();
        etPrice.setText(Double.toString(price));
        categoryId = product.getCategory_id();
        CRUDDialog.setSpinnerSelector(spCategory, categoryId);
        unitId = product.getUnit_id();
        CRUDDialog.setSpinnerSelector(spUnit, unitId);
    }
}
